import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';

// 定义路由组件
const Home = () => import('@/views/Home.vue');
const Login = () => import('@/views/Login.vue');
const Register = () => import('@/views/Register.vue');
const Profile = () => import('@/views/Profile.vue');

// 定义路由规则
const routes: Array<RouteRecordRaw> = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: Home, meta: { title: '首页' } },
  { path: '/login', component: Login, meta: { title: '登录' } },
  { path: '/register', component: Register, meta: { title: '注册' } },
  { path: '/profile', component: Profile, meta: { title: '个人中心' } }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
});

// 导航守卫，用于设置页面标题
router.beforeEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }
  return true;
});

export default router;